﻿using Autofac;
using WebMvc.App_Start;
using System;
using System.Diagnostics;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;
using Autofac.Integration.Mvc;
 
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
 
using WebMvc.Models;
using MicroOrm.Dapper.Repositories.SqlGenerator;
using DoMain.IdentityUsers;
using DoMain.IdentityRoles;
using DoMain.IdentityUserClaims;
using DoMain.IdentityUserLogins;
using DoMain.IdentityUserRoles;
 
using Repository.Dapper;

namespace WebMvc
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {
            FilterConfig.Configure(GlobalFilters.Filters);
            RouteConfig.Configure(RouteTable.Routes);
            GlobalConfiguration.Configure(WebApiConfig.Register);



            AutofacResolver();

            
           


        }
        private void AutofacResolver()
        {
            // MVC setup documentation here:
            // http://autofac.readthedocs.io/en/latest/integration/mvc.html
            // WCF setup documentation here:
            // http://autofac.readthedocs.io/en/latest/integration/wcf.html
            //
            // First we'll register the MVC/WCF stuff...
            var builder = new ContainerBuilder();

            // MVC - Register your MVC controllers.
            builder.RegisterControllers(typeof(Global).Assembly);

            // MVC - OPTIONAL: Register model binders that require DI.
            builder.RegisterModelBinders(typeof(Global).Assembly);
            builder.RegisterModelBinderProvider();

            // MVC - OPTIONAL: Register web abstractions like HttpContextBase.
            builder.RegisterModule<AutofacWebTypesModule>();

            // MVC - OPTIONAL: Enable property injection in view pages.
            builder.RegisterSource(new ViewRegistrationSource());

            // MVC - OPTIONAL: Enable property injection into action filters.
            builder.RegisterFilterProvider();

            // Register application dependencies.

            //Database connection
            var connectionString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
            builder.Register(c => new SqlConnection(connectionString)).As<IDbConnection>().InstancePerLifetimeScope();

            //Sql generators
            builder.RegisterType<SqlGenerator<IdentityUser>>().As<ISqlGenerator<IdentityUser>>().SingleInstance();
            builder.RegisterType<SqlGenerator<IdentityRole>>().As<ISqlGenerator<IdentityRole>>().SingleInstance();

            builder.RegisterType<SqlGenerator<IdentityUserClaim>>().As<ISqlGenerator<IdentityUserClaim>>().SingleInstance();
            builder.RegisterType<SqlGenerator<IdentityUserLogin>>().As<ISqlGenerator<IdentityUserLogin>>().SingleInstance();
            builder.RegisterType<SqlGenerator<IdentityUserRole>>().As<ISqlGenerator<IdentityUserRole>>().SingleInstance();
            


            //User
            builder.RegisterType<IdentityUserRepository>().As<IIdentityUserRepository>();

            //Role
            builder.RegisterType<IdentityRoleRepository>().As<IIdentityRoleRepository>();
            //Role
            builder.RegisterType<IdentityUserClaimRepository>().As<IIdentityUserClaimRepository>();
            //Role
            builder.RegisterType<IdentityUserLoginRepository>().As<IIdentityUserLoginRepository>();
            //Role
            builder.RegisterType<IdentityUserRolesRepository>().As<IIdentityUserRoleRepository>();



            DependencyResolver.SetResolver(new AutofacDependencyResolver(builder.Build()));

           // ControllerBuilder.Current.SetControllerFactory(new MyControllerFactory(container));
        }


        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {
            Exception ex = Server.GetLastError();
            if (ex != null)
            {
                Trace.TraceError(ex.ToString());
            }
        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}